package com.ferapps.tecsa;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.NumberPicker;
import android.widget.ToggleButton;

public class MainActivity extends Activity {

    private static String[] displayedValuesPicker1=new String[]{"ON", "OFF", "Stato", "Evento", "Credito" };
    private static String[] displayedValuesPicker2=new String[]{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
    private static String[] displayedValuesPicker3=new String[]{"Centrale 1", "Centrale 2", "Centrale 3" };

    private ToggleButton tgImp, tgAree, tgIngr, tgUsc;
    private NumberPicker npPicker2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_main);

	// Get references
	final EditText genString=(EditText) findViewById(R.id.genString);

	tgImp=(ToggleButton) findViewById(R.id.toggleButtonImp);
	tgAree=(ToggleButton) findViewById(R.id.toggleButtonAree);
	tgIngr=(ToggleButton) findViewById(R.id.toggleButtonIngr);
	tgUsc=(ToggleButton) findViewById(R.id.toggleButtonUsc);

	// Set number pickers
	NumberPicker picker1=(NumberPicker) findViewById(R.id.numberPicker1);
	picker1.setMinValue(0);
	picker1.setMaxValue(4);
	picker1.setDisplayedValues(displayedValuesPicker1);
	picker1.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
	    @Override
	    public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
		genString.setText("" + displayedValuesPicker1[newVal]);
		if (newVal == 3) {
		    npPicker2.setVisibility(View.VISIBLE);
		}
		else {
		    npPicker2.setVisibility(View.GONE);
		}
	    }
	});

	npPicker2=(NumberPicker) findViewById(R.id.numberPicker2);
	npPicker2.setMinValue(0);
	npPicker2.setMaxValue(9);
	npPicker2.setDisplayedValues(displayedValuesPicker2);

	NumberPicker picker3=(NumberPicker) findViewById(R.id.numberPicker3);
	picker3.setMinValue(0);
	picker3.setMaxValue(2);
	picker3.setDisplayedValues(displayedValuesPicker3);

	// Set button listeners
	final Button buttonSend=(Button) findViewById(R.id.buttonSend);
	buttonSend.setOnClickListener(new View.OnClickListener() {
	    @Override
	    public void onClick(View v) {
		// Perform action on click
	    }
	});

	final Button buttonPrefs=(Button) findViewById(R.id.buttonPrefs);
	buttonPrefs.setOnClickListener(new View.OnClickListener() {
	    @Override
	    public void onClick(View v) {
		Intent intent=new Intent(getApplicationContext(), PrefsActivity.class);
		startActivity(intent);
	    }
	});

	final Button buttonSetup=(Button) findViewById(R.id.buttonSetup);
	buttonSetup.setOnClickListener(new View.OnClickListener() {
	    @Override
	    public void onClick(View v) {
		Intent intent=new Intent(getApplicationContext(), SetupActivity.class);
		startActivity(intent);
	    }
	});

	final Button buttonPrefsAdd=(Button) findViewById(R.id.buttonPrefsAdd);
	buttonPrefsAdd.setOnClickListener(new View.OnClickListener() {
	    @Override
	    public void onClick(View v) {
		Intent intent=new Intent(getApplicationContext(), PrefsAddActivity.class);
		startActivity(intent);
	    }
	});
    }

    public void onToggleClicked_Imp(View view) {
	// Is the toggle on?
	boolean on=((ToggleButton) view).isChecked();

	if (on) {
	    tgAree.setChecked(false);
	    tgIngr.setChecked(false);
	    tgUsc.setChecked(false);
	}
	else {
	    // Disable vibrate
	    tgImp.setChecked(true);
	}
    }

    public void onToggleClicked_Aree(View view) {
	// Is the toggle on?
	boolean on=((ToggleButton) view).isChecked();

	if (on) {
	    tgImp.setChecked(false);
	    tgIngr.setChecked(false);
	    tgUsc.setChecked(false);
	}
	else {
	    // Disable vibrate
	    tgAree.setChecked(true);
	}
    }

    public void onToggleClicked_Ingr(View view) {
	// Is the toggle on?
	boolean on=((ToggleButton) view).isChecked();

	if (on) {
	    tgAree.setChecked(false);
	    tgImp.setChecked(false);
	    tgUsc.setChecked(false);
	}
	else {
	    // Disable vibrate
	    tgIngr.setChecked(true);
	}
    }

    public void onToggleClicked_Usc(View view) {
	// Is the toggle on?
	boolean on=((ToggleButton) view).isChecked();

	if (on) {
	    tgAree.setChecked(false);
	    tgIngr.setChecked(false);
	    tgImp.setChecked(false);
	}
	else {
	    // Disable vibrate
	    tgUsc.setChecked(true);
	}
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
	// Inflate the menu; this adds items to the action bar if it is present.
	getMenuInflater().inflate(R.menu.main, menu);
	return true;
    }

}
